//
//  WholeAnimViewController.m
//  YXLearnMasonry
//
//  Created by Rookie_YX on 16/10/27.
//  Copyright © 2016年 Rookie_YX. All rights reserved.
//

#import "WholeAnimViewController.h"

@interface WholeAnimViewController ()
@property (nonatomic, strong) UIView *bigView;
@property (nonatomic, strong) UIView *smallView;
@property (nonatomic, strong) UIView *minimumView;
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, assign) BOOL isExpand;
@end

@implementation WholeAnimViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  self.bigView = [UIView new];
  self.bigView.backgroundColor = [UIColor orangeColor];
  [self.view addSubview:_bigView];
  
  self.smallView = [UIView new];
  self.smallView.backgroundColor = [UIColor greenColor];
  [self.view addSubview:_smallView];
  
  self.minimumView = [UIView new];
  self.minimumView.backgroundColor = [UIColor cyanColor];
  [self.view addSubview:_minimumView];
  
  self.btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  [self.btn setTitle:@"点击这里视图会整体放大缩小" forState:(UIControlStateNormal)];
  self.btn.titleLabel.font = [UIFont systemFontOfSize:12];
  [self.btn addTarget:self action:@selector(clickBtn:) forControlEvents:(UIControlEventTouchUpInside)];
  [self.view addSubview:self.btn];
  
  // 布局
  [self.bigView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.top.equalTo(@30);
    make.trailing.bottom.equalTo(@-30);
  }];
  
  [self.smallView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.top.equalTo(self.bigView).offset(50);
    make.trailing.bottom.equalTo(self.bigView).offset( -50);
  }];
  
  [self.minimumView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.top.equalTo(self.smallView).offset(30);
    make.trailing.bottom.equalTo(self.smallView).offset( -30);
  }];
  
  [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.bigView);
    make.leading.equalTo(self.bigView).offset(40);
    make.trailing.equalTo(self.bigView).offset(-40);
    make.height.equalTo(50);
  }];
}

- (void)clickBtn:(UIButton *)button{
  [self updateWithExpand:!self.isExpand animated:YES];
}

- (void)updateWithExpand:(BOOL)isExpanded animated:(BOOL)animated{
  self.isExpand = isExpanded;
  
  [self.bigView updateConstraints:^(MASConstraintMaker *make) {
    if (isExpanded) {
      make.leading.top.equalTo(@80);
      make.trailing.bottom.equalTo(@-80);
      self.btn.titleLabel.font = [UIFont systemFontOfSize:9];
    }else{
      make.leading.top.equalTo(@30);
      make.trailing.bottom.equalTo(@-30);
      self.btn.titleLabel.font = [UIFont systemFontOfSize:12];
    }
  }];
  
  if (animated) {
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.5 animations:^{
      [self.view layoutIfNeeded];
    }];
  }
  
}
@end
